package de.distmlp.preprocessing.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.mahout.common.RandomUtils;

import de.distmlp.preprocessing.data.MLDataEntry;
import de.distmlp.preprocessing.nlp.dictionary.Dictionary;

/**
 * Maps input to output to train the identity with autoencoder.
 * 
 * @author twist
 * 
 */
public class XingParser_Denoising extends XingParser {

	private final double delRatio = 0.5, onlyOneInputRatio = 0.2;
	private final Random gen = RandomUtils.getRandom();

	public XingParser_Denoising(final Dictionary dictionary, final int nbInputUnits, final int nbOutputUnits) {
		super(dictionary, nbInputUnits, nbOutputUnits);
	}

	@Override
	public List<MLDataEntry> parse(final String input) {
		final List<MLDataEntry> trainingsSet = new ArrayList<MLDataEntry>();

		final Map<String, Integer> dicMap = this.dictionary.getDictionary();
		final String[] lineSplit = input.split(",");

		// Dictionary does not contain all lemmas. We need to check
		// if lemma exists in dictionary,

		final MLDataEntry trainingsData = new MLDataEntry(this.nbInputUnits, this.nbOutputUnits);

		final List<String> data = new ArrayList<String>();

		if (lineSplit.length > 1) {
			for (final String element : lineSplit) {
				if (dicMap.containsKey(element)) {
					data.add(element);
				} else {
					continue;
				}
			}
		}

		if (data.size() < 2) {
			return trainingsSet;
		}

		for (int i = 0; i < data.size(); i++) {
			trainingsData.addOutput(dicMap.get(data.get(i)), 1);
		}

		if (this.onlyOneInputRatio > this.gen.nextDouble()) {

			trainingsData.addInput(dicMap.get(data.get(this.gen.nextInt(data.size()))), 1);
		} else {
			final int maxDeleted = (int) (data.size() * this.delRatio);
			for (int j = 0; j < maxDeleted; j++) {
				trainingsData.addInput(dicMap.get(data.get(this.gen.nextInt(data.size()))), 1.0);
			}
		}

		trainingsSet.add(trainingsData);

		return trainingsSet;
	}

}
